package com.koron.standard.riskStock.controller;

import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.common.core.business.common.service.TOrderNoMaxService;
import com.koron.standard.riskStock.bean.param.RiskMergeParam;
import com.koron.standard.riskStock.bean.po.RiskStock;
import com.koron.standard.riskStock.bean.query.RiskStockQuery;
import com.koron.standard.riskStock.bean.vo.RiskStockVo;
import com.koron.standard.riskStock.excel.RiskStockListener;
import com.koron.standard.riskStock.service.RiskStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author jzp
 * @since 2021/9/1
 */
@Api(tags = "风险库接口")
@RestController
@RequestMapping("riskStock")
public class RiskStockController {

    @Autowired
    RiskStockService riskStockService;

    @Autowired
    TOrderNoMaxService tOrderNoMaxService;

    @GetMapping
    @ApiOperation(value = "根据id查询")
    public Response<RiskStock> select(@RequestParam String id) {
        RiskStock riskStock = riskStockService.getById(id);
        return Response.ok(riskStock);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Response<Page<RiskStockVo>> page(RiskStockQuery<RiskStockVo> pageQuery) {
        Page<RiskStockVo> page = riskStockService.page(pageQuery);
        return Response.succ(page, page.getTotal());
    }

    @GetMapping("list")
    @ApiOperation(value = "列表查询")
    public Response<List<RiskStockVo>> list(RiskStockQuery<RiskStockVo> pageQuery) {
        List<RiskStockVo> list = riskStockService.list(pageQuery);
        return Response.ok(list);
    }

    @GetMapping("listByParent")
    @ApiOperation(value = "分组查询风险类别", notes = "如果typeName未空，则查询父类，如果不为空，则以typeName未父类来查询子类")
    public Response<List<RiskStock>> listByParent(String typeName) {
        return Response.ok(riskStockService.listByParent(typeName));
    }

    @GetMapping("ref")
    @ApiOperation(value = "引用权重", notes = "将这条风险库数据的引用数加1")
    public Response ref(String id) {
        riskStockService.incrementRef(id);
        return Response.ok("引用+1", null);
    }

    @PostMapping
    @ApiOperation(value = "保存")
    public Response saveOrUpdate(@RequestBody RiskStock riskStock) {
        riskStockService.saveOrUpdate(riskStock);
        return Response.ok(riskStock.getId());
    }

    @PutMapping("merge")
    @ApiOperation(value = "合并数据")
    public Response merge(@RequestBody @Valid RiskMergeParam riskMergeParam) {
        riskStockService.merge(riskMergeParam);
        return Response.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public Response delete(@RequestParam("ids") List<String> ids) {
        boolean b = riskStockService.removeByIds(ids);
        return Response.flag(b);
    }

    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/risk_stock_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("风险库", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        book.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        EasyExcel.read(file.getInputStream(), RiskStock.class, new RiskStockListener(riskStockService, tOrderNoMaxService))
                .sheet("风险库")
                .headRowNumber(1)
                .doRead();

        return Response.ok("导入成功", null);
    }

    @SneakyThrows
    @ApiOperation(value = "导出", notes = "根据条件导出查询出的所有数据")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, RiskStockQuery<RiskStockVo> pageQuery) {
        String path = "excel/risk_stock_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("风险库", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<RiskStockVo> riskStockVoList = riskStockService.list(pageQuery);
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                .needHead(false)
                .sheet("风险库")
                .doWrite(riskStockVoList);
    }

}
